@extends('layouts.app')

@section('content')
    <div class="row">
        <div class="col-xs-12 col-md-10 col-lg-8">
            <!-- BEGIN PAGE HEADER -->
            <div class="clearfix">
                <h3 class="page-title pull-left">
                    <a class="a-unstyled" href="/admin/admins">
                        <i class="icon-arrow-left-circle"></i>
                    </a>
                    编辑管理员
                </h3>
            </div>
            <!-- END PAGE HEADER -->
        </div>

        <div class="col-xs-12 col-md-10 col-lg-8">
            <!-- BEGIN PAGE CONTENT-->
            <form action="/admin/admins/{{ $admin->id }}" method="post" novalidate>

                {{ csrf_field() }}

                {{ method_field('put') }}

                <div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">

                    <label for="field_name">@lang('t.name')</label>
                    <input type="text" id="field_name" class="form-control"
                           name="name" required value="{{ old('name', $admin->name) }}">

                    @if ($errors->has('name'))
                        <span class="help-block">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">

                    <label for="field_email">@lang('t.email')</label>
                    <input type="text" id="field_email" class="form-control"
                           name="email" required value="{{ old('email', $admin->email) }}">

                    @if ($errors->has('email'))
                        <span class="help-block">
                            <strong>{{ $errors->first('email') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group {{ $errors->has('permission_ids') ? 'has-error' : '' }}">

                    <label for="field_permission_ids">@lang('t.permission')</label>

                    <div class="row">
                        @foreach($permissions as $permission)
                            <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                <label>
                                    <input type="checkbox"
                                           name="permission_ids[]" value="{{ $permission->id }}"

                                        {{-- 如果权限字段报错, 只会是用户没有勾, 就不将用户已有的权限与所有权限比对 --}}
                                        {{  !$errors->first('permission_ids') && $admin->permissions->contains($permission) ? 'checked' : '' }}
                                    >
                                    {{ $permission->description }}
                                </label>
                            </div>
                        @endforeach
                    </div>

                    @if ($errors->has('permission_ids'))
                        <span class="help-block">
                            <strong>{{ $errors->first('permission_ids') }}</strong>
                        </span>
                    @endif
                </div>

                @if ($errors->has('general'))
                    <div class="form-group has-error">
                        <span class="help-block">
                            <strong>{{ $errors->first('general') }}</strong>
                        </span>
                    </div>
                @endif

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-submit">提交</button>
                </div>
            </form>
            <!-- END PAGE CONTENT-->
        </div>
    </div>
@endsection

